﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


class StudentsLibrary
{
    static void Main(string[] args)
    {
        string fileName = "students.txt";
        File.Delete(fileName);
        Student elena = new Student("Elena", "Peneva", "fn80135");
        Student iva = new Student("Iva", "Toteva", "fn80132");
        Student georgi = new Student("Georgi", "Todorov", "fn80185");

        AddStudent(elena, fileName);
        AddStudent(iva, fileName);
        AddStudent(georgi, fileName);
        PrintAllStudents(fileName);

        DeleteStudent(fileName, elena);
        PrintAllStudents(fileName);

        AddStudent(new Student("Nikolaj", "Andreev", "55434"), fileName);
        PrintAllStudents(fileName);

    }

    static void AddStudent(Student student, string studentsLibraryFile)
    {
        FileStream outFile = new FileStream(studentsLibraryFile, FileMode.Append, FileAccess.Write);
        using (outFile)
        {
            BinaryWriter writer = new BinaryWriter(outFile);
            using (writer)
            {
                writer.Write(student.ToString());
            }
        }
    }

    static void PrintAllStudents(string studentsLibraryFile)
    {
        Console.WriteLine("------------printing all students----------------");
        FileStream inFile = new FileStream(studentsLibraryFile, FileMode.OpenOrCreate, FileAccess.Read);
        using (inFile)
        {
            BinaryReader reader = new BinaryReader(inFile);
            using (reader)
            {
                while(reader.PeekChar() != -1)
                {
                    string studentInfo = reader.ReadString();
                   // Console.WriteLine("reading....   " + studentInfo);
                    Student student = new Student(studentInfo);
                    Console.WriteLine("First Name: {0}\nLast Name: {1}\nFaculty Number: {2}\n",
                        student.FirstName, student.LastName, student.FacultyNumber);
                }
            }
        }
    }

    static void DeleteStudent(string studentsLibraryFile, Student studentToBeDeleted)
    {
        FileStream inFile = new FileStream(studentsLibraryFile, FileMode.OpenOrCreate, FileAccess.Read);
        using (inFile)
        {
            BinaryReader reader = new BinaryReader(inFile);
            using (reader)
            {
                List<Student> students = new List<Student>();
                while (reader.PeekChar() != -1)
                {
                    string studentInfo = reader.ReadString();
                    Student student = new Student(studentInfo);
                    if (!student.Equals(studentToBeDeleted))
                    {
                        students.Add(student);
                    }
                }
                reader.Close();
                File.Delete(studentsLibraryFile);
                WriteStudents(students, studentsLibraryFile);
            }
        }
    }

    static void WriteStudents(List<Student> students, string studentLibraryFile)
    {   
        foreach (Student student in students)
        {
            AddStudent(student, studentLibraryFile);
        }
    }
}

